package org.eduapp.trains.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import org.eduapp.trains.Constants;

/**
 * 
 * @author JeccyZhao
 *
 */
public class UtilsResources {
	
	/**
	 * 获取ClassPath路径
	 * @return
	 * @author JeccyZhao
	 */
	public static String getWebClassPath(){
		String classPath = UtilsResources.class.getResource("/").toString();
		if(classPath.indexOf(Constants.RESOURCE_FILE_PREFIX) > -1){
			classPath = classPath.substring(Constants.RESOURCE_FILE_PREFIX.length());
		}
		return classPath;
	}
	
	/**
	 * 获取Web根路径
	 * @return
	 * @author JeccyZhao
	 */
	public static String getWebRootPath(){
		return getWebRootPath(null);
	}
	
	/**
	 * 
	 * @param path
	 * @return
	 */
	public static String getWebRootPath (String path) {
		String classPath = UtilsResources.getWebClassPath();
		String rootPath = "";
		if(classPath.indexOf(Constants.RESOURCE_WEB_INF) > -1){
			rootPath = classPath.substring(0,classPath.indexOf(Constants.RESOURCE_WEB_INF));
		}
		if (UtilsString.isNotEmpty(path)) {
			rootPath = rootPath + path;
		}
		
		File file = new File(rootPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		
		return rootPath;
	}
	
	/**
	 * 
	 * @param path
	 * @return
	 * @author JeccyZhao
	 */
	public static File[] getFiles(String path){
		if(path.indexOf(Constants.RESOURCE_CLASSPATH_PREFIX) > -1){
			path = path.replaceFirst(
					Constants.RESOURCE_CLASSPATH_PREFIX, UtilsResources.getWebClassPath()); 
		}
		File file = new File(path);
		return file.exists() ? file.listFiles() : null;
	}
	
	/**
	 * 
	 * @param path
	 * @return
	 * @author JeccyZhao
	 */
	public static String[] getFilePaths(String path){
		if(path.indexOf(Constants.RESOURCE_CLASSPATH_PREFIX) > -1){
			path = path.replaceFirst(
					Constants.RESOURCE_CLASSPATH_PREFIX, UtilsResources.getWebClassPath()); 
		}
		File[] files = UtilsResources.getFiles(path);
		if (files != null && files.length > 0){
			String[] paths = new String[files.length];
			int count = 0;
			for(File file : files){
				paths[count++] = file.getPath();
			}
			return paths;
		}
		return null;
	}
	
	/**
	 * 
	 * @param propFilePath  format can be classpath:xxxx,etc.
	 * @return
	 * @author JeccyZhao
	 */
	public static Map<String, String> getProperties(String propFile){
		Properties props = new Properties();
		InputStream stream = null;
		try {
			if(propFile.indexOf(Constants.RESOURCE_CLASSPATH_PREFIX) > -1){
				propFile = propFile.replaceFirst(
						Constants.RESOURCE_CLASSPATH_PREFIX, UtilsResources.getWebClassPath()); 
			}
			stream = new FileInputStream(new File(propFile));
			props.load(stream);
			Map<String, String> propMaps = new HashMap<String, String>();
			for(Entry<Object, Object> entry : props.entrySet()){
				if(entry.getKey() != null){
					propMaps.put(entry.getKey().toString(), entry.getValue().toString());
				}
			}
			return propMaps;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(stream != null){
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	public static void main(String[] args){
		System.out.println(UtilsResources.getFilePaths(Constants.RESOURCE_URLREWRITE));
		System.out.println(UtilsResources.getWebClassPath());
		System.out.println(UtilsResources.getWebRootPath());
	}
	
}
